/* Copyright 2012 Nathan Begle
    This file is part of pcoursemanager.

    pcoursemanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pcoursemanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pcoursemanager.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;


public class Class_Connection {
	private URLConnection connection;
	private URL url;
	private Pattern pat;
	private Matcher match;
	private BufferedReader datain;
	public boolean available;
	public int capacity;
	public int actual;
	public int remaining;
	
	public Class_Connection(long crn, int semester, int year)
		throws Exception
	{
		if(semester != 1)
		{
			url = new URL(String.format("https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_disp_detail_sched?term_in=%d%d0&crn_in=%d", year, semester, crn));
		} else {
			url = new URL(String.format("https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_disp_detail_sched?term_in=%d%d0&crn_in=%d", year+1, semester, crn));
		}
		capacity = 0;
		actual = 0;
		remaining = 0;
		available = false;
		pat = Pattern.compile("-?\\d+");
	}
	
	private void open_connection()
		throws Exception
	{
		connection = url.openConnection();
	}
	
	public void get_data()
		throws Exception
	{
		open_connection();
		datain = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		
		parse_data();
		datain.close();
	}
	
	private void parse_data()
	throws Exception
	{
		String tempStr;
		String dataLine;
		
			while(!(datain.readLine().contains("Seats"))){}
		
			dataLine = datain.readLine();
			match = pat.matcher(dataLine);
			match.find();
			tempStr = match.group();
			capacity = Integer.parseInt(tempStr);
			
			dataLine = datain.readLine();
			match = pat.matcher(dataLine);
			match.find();
			tempStr = match.group();
			actual = Integer.parseInt(tempStr);
			
			dataLine = datain.readLine();
			match = pat.matcher(dataLine);
			match.find();
			tempStr = match.group();
			remaining = Integer.parseInt(tempStr);
			
			if(remaining > 0) available = true;
			else available = false;
	}
	
	public void print(int semester, int year, long crn)
	{
		if(semester == 1)
		{
			System.out.println(String.format("Fall %d -- CRN = %d", year, crn));
		}
		else if(semester == 2)
		{
			System.out.println(String.format("Spring %d -- CRN = %d", year, crn));
		}
		else if(semester == 3)
		{
			System.out.println(String.format("Summer %d -- CRN = %d", year, crn));
		}
		
		if((capacity - actual) != remaining)
		{
			System.out.println("Error: Cap - Actual != rem");
		}
		System.out.println(String.format("capacity = %d, actual = %d, remaining = %d", capacity, actual, remaining));
		if(available)
		{
			System.out.println("Available");
		}
		else
		{
			System.out.println("Not Available");
		}
	}
}
